<?php require ('setcookie.html'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="de">
<head>
    <title>MP3 Vokabeltrainer - Vokabellisten - Lingomoto</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="MP3 Vokabeltrainer - Vokabellisten - Sprachen lernen" />
    <meta name="language" content="de" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
</head>

<body id="trainer">
<?php
function b(){print '<BR>';}
function mysql_query_secure($query)
{
   $result=mysql_query($query);print $query;
   if ( ! $result ) { die('Error query : ' . mysql_error()); }
   return $result;
}
function language_info()
{
       if (TESTING)echo 'language_info()';
	   global $customerid;
	   $result = mysql_query_secure( "SELECT can,learn,langpair FROM settings where customerid = '$customerid'" );
       if (mysql_num_rows($result)<>0) $line = mysql_fetch_array( $result, MYSQL_ASSOC);
	   return array($line['can'],$line['learn'],$line['langpair']);

}
function create_table($first_lang,$second_lang)
{ $query="CREATE TABLE IF NOT EXISTS `db1153130-testdb2`.`$first_lang$second_lang` (
  `wordid` int(2) NOT NULL,
  `canart` varchar(3)  NOT NULL,
  `canword` varchar(100)  NOT NULL,
  `canid` int(1) NOT NULL,
  `learnart` varchar(3) NOT NULL,
  `learnword` varchar(100)  NOT NULL,
  `learnid` int(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$result=mysql_query_secure($query);
}
function num_dicts($first_lang,$second_lang)
{   $query=" SELECT DISTINCT `language_combo` 
                   FROM `alltranslations`
                   WHERE `language_combo` LIKE '{$first_lang}_{$second_lang}%'";print $inner_query;
   $result=mysql_query_secure($query);
   $num_dicts=mysql_num_rows($result);
   return $num_dicts;
}
function eval_frequency_and_order($frequency_array,$translations) //bei gleich häufigen Übersetzungen (mehr als einmal)
{ //Übersetzung die in allen Wörterbüchern durchschnittlich eher vorne liegt
   print 'function eval_frequency_and_order';b();
   foreach ($frequency_array as $translation=>$count)
	{   if ($last_count<>'' and $last_count<>$count)
		{    $frequency_parted_array[]=$parted_array;
             $parted_array=array();	
		}
	$parted_array[$translation]=$count;
	$last_count=$count;
	}
	$frequency_parted_array[]=$parted_array;	
	print_r($frequency_parted_array);

	foreach ($frequency_parted_array as $parted_array)
	{
	  foreach ($parted_array as $translation=>$count)
		{   $position_sum=0; 
		    $position_count=0;
		    foreach ($translations as $translation_array)
				{    if (in_array($translation,$translation_array))
							{     $position_sum+=array_search($translation,$translation_array);
									$position_count+=1;
							}
				}			
			$position_average=	 $position_sum/$position_count;
			$position_array[$translation]=$position_average;		 
				
		}
	print_r ($position_array);
	asort($position_array);
	foreach ($position_array as $translation=>$position_average)
		{    $translation_count = $frequency_array[$translation];
			$new_frequency_array[$translation]=$translation_count;

		}
	 $position_array=array();	
	}

	print_r($new_frequency_array);
	return $new_frequency_array;
}
function eval_final_translations($frequency_array,$translations)
{   print 'function eval_final_translations';b();
//1.�blichste �bersetzungen
//2.�bersetzungen, die h�ufiger im Sprachgebrauch sind

//Funktion zur Bestimmung der Abstands-Wertigkeit von 2 W�rtern derselben Liste
//index1/(index1-index2) > 4,5 //index1 ist die gr��ere Zahl  
$final_translations=array();
$frequency_array=eval_frequency_and_order($frequency_array,$translations);
foreach ($frequency_array as $translation=>$translation_count)
//for ($a=0;$a<=2;$a++)
{
  if (count($final_translations)<3)
  {  if ($translation_count>1) //Vokabel kommt als �bersetzung in mehreren W�rterb�chern vor-> gute �bersetzung, �bernehmen
	 {  $final_translations[]=$translation;
 	 }
     else 
	 {  	$possible_translations[]=$translation;
			
     }
  }
}
//herausfinden in welchen W�rterb�chern die bereits genommenen W�rter drin sind
		// diese W�rterb�cher sind 'weniger geeignet', k�nnen sich aber durch sehr gute Wortpositionierung
		//in mostcommonwords trotzdem noch einbauen
$unfit=array();
foreach ($final_translations as $final_translation)
{   $count=0;
    foreach ($translations as $translation_array)
	{  if (in_array($final_translation,$translation_array)) $unfit[]=$count;
			   $count+=1;
	}
}  	

$remaining=array();
$counted_translations=count($final_translations);
if ($counted_translations<3 and $possible_translations) 
{
   $remaining=rank_remaining_translations(3-$counted_translations,$unfit,$possible_translations,$translations);
}   
$final_translations=array_merge($final_translations,$remaining);
return $final_translations;
}
function order_by_index($possible_translations)
{  print 'function order_by_index';b();
   global $second_lang;
   foreach ($possible_translations as $possible_translation)
   {   $result=mysql_query_secure("SELECT `index` FROM mostcommonwords WHERE language='$second_lang' and word='$possible_translation'");
       if ($row=mysql_fetch_array($result)) $word_index=$row[0];else $word_index=30000;
	   $possible_array[$possible_translation]=$word_index;
   }
   asort($possible_array);
   return $possible_array;


}
function rank_remaining_translations($count_missing, $unfit,$possible_translations,$translations)
{  print 'function rank_remaining_translations';b();
   print '$possible_translations';b();//print_r ($possible_translations);
   foreach ($possible_translations as $possible_translation)
   {  if (is_unfit($possible_translation,$unfit,$translations))$unfit_possible_translations[]=$possible_translation;
	  else $fit_possible_translations[]=$possible_translation;
   }
   
   print '$fit_possible_translations';b();print_r ($fit_possible_translations); 
   print '$unfit_possible_translations';b();print_r ($unfit_possible_translations);
   if ($fit_possible_translations)$fit_possible_translations=order_by_index($fit_possible_translations);
   if ($unfit_possible_translations)$unfit_possible_translations=order_by_index($unfit_possible_translations);
   
   $remaining=find_best($fit_possible_translations,$unfit_possible_translations,$count_missing);
   return $remaining;
}
function get_value($count,$array)
{ $a=0; 
  if (!is_array($array))return false;
  foreach ($array as $value){if ($a==$count) return $value; $a+=1;}
  return false;
}
function get_key($count,$array)
{ $a=0;
  if (!is_array($array))return false;
  foreach ($array as $key=>$value){if ($a==$count) return $key; $a+=1;}
  return false;
}
function find_best($fit,$unfit,$count_missing)
{   print 'function find_best';b();
    $count_fit=0;$count_unfit=0;
    for ($a=0;$a<$count_missing;$a++)
    {    $fit_value=get_value($count_fit,$fit);$unfit_value=get_value($count_unfit,$unfit);
	    if (!$unfit_value)       {$remaining[]=get_key($count_fit,$fit);$count_fit+=1;}
		elseif (!$fit_value) {$remaining[]=get_key($count_unfit,$unfit);$count_unfit+=1;}
		//print $fit_value/($fit_value-$unfit_value);b();
		//print $unfit_value;b();
        elseif ($fit_value<$unfit_value) {$remaining[]=get_key($count_fit,$fit);$count_fit+=1;}
        elseif ($fit_value/($fit_value-$unfit_value) < 4.5)  {$remaining[]=get_key($count_unfit,$unfit);$count_unfit+=1;}
		else {$remaining[]=get_key($count_fit,$fit);$count_fit+=1;}
    }
	return $remaining;
}
function is_unfit($possible_translation,$unfit, $translations)
{   print 'function is_unfit';b();
    foreach ($translations as $key=>$translation)
    {   if (in_array($possible_translation,$translation) and in_array($key,$unfit)) {print 'is_unfit'; return true;}
    }
    return false;
}
function insert_translations($first_lang,$second_lang,$translations,$word)
{
    foreach ($translations as $translation) 
    {  $query="INSERT INTO $first_lang$second_lang (canword, learnword) VALUES ('$word','$translation')";
       $result=mysql_query_secure($query);
    }
}   
$_SESSION['logini'] = isset($_SESSION['logini']) ? $_SESSION['logini'] : false;
// read configuration
require ('konfiguration.html');
// read user-data
require ('nutzer.html');
// read header
$link = "vokabellisten.html";
$langen = 'words-lists.html';
$langes = 'listas-vocablos.html';
$langru = 'vokabellisten.html';
require ('kopf.html');
$customerid = $_SESSION['customerid'];
$lang_info=language_info();
$can_lang=$lang_info[0]; //'german'
$learn_lang=$lang_info[1];
$langpair=$lang_info[2];
mysql_query("SET NAMES 'utf8'");

$first_lang="english";
$second_lang="spanish";

create_table($first_lang,$second_lang); //Datenbank-tabelle für Sprachkombination erstellen
$num_dicts=num_dicts($first_lang,$second_lang); //Anzahl der W�rterb�cher f�r diese Sprachkombination herausfinden
$query="SELECT * from mostcommonwords WHERE language='$first_lang' ORDER BY `index`";
$result=mysql_query_secure($query);
while ($row = mysql_fetch_array($result) and $count <5   /*TESTING */) //h�ufigste W�rter der ersten Sprache einzeln durchgehen
{
   $word=$row['word'];   
   $translations=array();
   $translations_unordered=array();
   
   for ($a=0;$a<=$num_dicts;$a++) //alle �bersetzungen in ein Array packen, geordnet nach jeweiligem W�rterbuch
   {  if ($a==0)$a='';
      $inner_query="SELECT * from alltranslations WHERE language_combo='{$first_lang}_{$second_lang}$a' 
	                     AND word='$word'";
      $inner_result=mysql_query_secure($inner_query);
	  while ($row=mysql_fetch_array($inner_result))
	  {  $translations[$a][]=$row['translation'];             // [0]=[celebrate,party] [1]=[party]
	     $translations_unordered[]=$row['translation']; // [celebrate,party,party]
	  }
   }
   print 'print_r($translations);';b(); print_r($translations);
   print 'print_r($translations_unordered);';b(); print_r($translations_unordered);
   $frequency_array= array_count_values($translations_unordered); //print_r($frequency_array);
   arsort($frequency_array); //reversesort= von großen zu kleinen Zahlen, Ordnung nach Value 
   print 'print_r($frequency_array);'; b();print_r($frequency_array);// [party=2,celebrate=1]
   $final_translations=eval_final_translations($frequency_array,$translations);
   
   insert_translations($first_lang,$second_lang,$final_translations,$word);
} 





?>